<?php
header('Content-Type: application/json; charset: UTF-8');

include_once(dirname(__FILE__).'/config.php');
$store = ARC2::getStore($arc_config);

$q = <<<EOT
SELECT ?collectionId ?thumb ?typeLabel ?collectionLabel WHERE {
  {?collectionId a <http://desanti.org/schemas/collections#Cahier> .
   <http://desanti.org/schemas/collections#Cahier> rdfs:label ?typeLabel .}
  UNION
  {?collectionId a <http://desanti.org/schemas/collections#Pochette> .
   <http://desanti.org/schemas/collections#Pochette> rdfs:label ?typeLabel .}
  UNION
  {?collectionId a <http://desanti.org/schemas/collections#Insert> .
   <http://desanti.org/schemas/collections#Insert> rdfs:label ?typeLabel .}
  ?collectionId rdfs:label ?collectionLabel .
  ?collectionId <http://desanti.org/schemas/collections#pages> ?pages .
  ?pages <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1> ?page .
  ?page <http://desanti.org/schemas/collections#thumbnail> ?thumb .
}
EOT;

$rs = $store->query($q);
if(!$store->getErrors()){
	$rows = $rs['result']['rows'];

	for($i = 0; $i < sizeof($rows); $i++){
		$items[$rows[$i]['collectionId']]['type'] = $rows[$i]['typeLabel'];
		$items[$rows[$i]['collectionId']]['id'] = $rows[$i]['collectionId'];
		$items[$rows[$i]['collectionId']]['label'] = $rows[$i]['collectionLabel'];
		$items[$rows[$i]['collectionId']]['thumbnail'] = $rows[$i]['thumb'];
		$items[$rows[$i]['collectionId']]['href'] = 'javascript:self.opener.DESANTI.newLine("' .
			$rows[$i]['collectionId'] . '"); self.opener.focus( )';
	}

	$items = array('items'=>array_values($items));

	print_r(json_encode($items));
}
?>
